<?php
namespace admin\controllers;

use common\models\Recommend;
use common\models\RecommendBook;
use Yii;

/**
 * 推荐位书籍管理
 */
class RecommendBookController extends BaseController
{
    public $modelClass = 'common\models\RecommendBook';
    public $searchModelClass = 'admin\models\RecommendBookSearch';

    /**
     * @inheritdoc
     */
    public function actions()
    {
        $actions = parent::actions();

        $actions['create']['redirect'] = ['index', 'recommend_id' => Yii::$app->request->get('recommend_id')];
        $actions['update']['redirect'] = ['index', 'recommend_id' => Yii::$app->request->get('recommend_id')];

        return $actions;
    }

    /**
     * @inheritdoc
     */
    public function breadcrumbs()
    {
        $breadcrumbs = [];

        $recommend = Recommend::findOne(Yii::$app->request->get('recommend_id'));

        foreach (['index', 'create', 'update'] as $actionID) {
            $breadcrumbs[$actionID][] = ['label' =>  '推荐位管理', 'url' => ['recommend/index']];
            $breadcrumbs[$actionID][] = ['label' => $recommend->title, 'url' => ['index', 'recommend_id' => $recommend->id]];
            $breadcrumbs[$actionID][] = $this->getPageTitle($actionID);
        }

        return $breadcrumbs;
    }

    /**
     * @inheritdoc
     */
    public function actionButtons()
    {
        return [
            [
                'label'   => '新增推荐位作品',
                'url'     => ['create', 'recommend_id' => Yii::$app->request->get('recommend_id')],
                'options' => ['class' => 'btn green'],
            ],
        ];
    }

    public function actionUp()
    {
        $id = Yii::$app->request->get('id');

        $books = RecommendBook::find();
        foreach ($books->each() as $book) {
            if ($book->id == $id) { //如果是置顶的分类
                $book->display_order = 255;
            } else {
                $book->display_order > 0 ?  $book->display_order -= 1 :  $book->display_order;
            }
            $book->save();
        }

        $this->back();
    }
}
